package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.cloud.gateway.api.message.constraint.schedule.ScheduleActions;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum ScheduleTypeInnerStruct implements EnumTrans<CompleteEntity.ScheduleTypeStruct, ScheduleTypeInnerStruct> {
	/**
	 * 默认属性
	 * 休眠唤醒 对应指令SetAllSenderPower
	 * 亮度控制 对应指令SetAllBrightness
	 * 加密控制 对应指令DownloadLicense
	 * 解密控制 对应指令DecryptDevice
	 */
	ScheduleTypeStructDefault(CompleteEntity.ScheduleTypeStruct.SenderDecrypt, null),
	SenderPower(CompleteEntity.ScheduleTypeStruct.SenderPower, NameStruct.SetAllSenderPowerInstruction.getAClass()),
	BrightnessCtrl(CompleteEntity.ScheduleTypeStruct.BrightnessCtrl, NameStruct.SetAllBrightnessInstruction.getAClass()),
	SenderEncrypt(CompleteEntity.ScheduleTypeStruct.SenderEncrypt, NameStruct.EncryptInstruction.getAClass()),
	SenderDecrypt(CompleteEntity.ScheduleTypeStruct.SenderDecrypt, NameStruct.DecryptInstruction.getAClass()),
	
	
	;
	private final Class<? extends ScheduleActions> aClass;
	private final Integer id;
	private final String name;
	private final CompleteEntity.ScheduleTypeStruct scheduleTypeStruct;
	private static final long serialVersionUID = 369147258369147258L;
	
	ScheduleTypeInnerStruct(CompleteEntity.ScheduleTypeStruct scheduleTypeStruct, Class<? extends ScheduleActions> aClass) {
		this.aClass = aClass;
		this.id = scheduleTypeStruct.getNumber();
		this.name = scheduleTypeStruct.name();
		this.scheduleTypeStruct = scheduleTypeStruct;
	}
	
	/**
	 * @param scheduleTypeInnerStruct
	 * @return 对应的类型
	 **/
	public static ScheduleTypeInnerStruct getScheduleTypeInnerStruct(CompleteEntity.ScheduleTypeStruct scheduleTypeInnerStruct) {
		Objects.requireNonNull(scheduleTypeInnerStruct, "scheduleTypeInnerStruct can not be null.");
		return Arrays.stream(ScheduleTypeInnerStruct.values())
				.filter(e -> e.name.equals(scheduleTypeInnerStruct.name()))
				.findFirst()
				.orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
	}
	
	@Override
	public CompleteEntity.ScheduleTypeStruct toProto() {
		return this.scheduleTypeStruct;
	}
	
	@Override
	public ScheduleTypeInnerStruct formProto(CompleteEntity.ScheduleTypeStruct scheduleTypeInnerStruct) {
		return getScheduleTypeInnerStruct(scheduleTypeInnerStruct);
	}
}

